package com.btkjgc.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.extend.AccountInfoModel;
import com.btkjgc.service.impl.AccountRecordManageService_Impl;
import com.btkjgc.service.intf.AccountRecordManageService_Intf;

@Controller
public class AccountRecordManageController
{
	static Logger logger = Logger.getLogger(AccountRecordManageController.class);
	
	@Autowired
	private AccountRecordManageService_Intf accountRecordManageService;
	
	@RequestMapping(value = "account_record_manage", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView getBusinessRecords(HttpServletRequest request,Integer type)
	{
		ModelAndView mv = new ModelAndView("account_record_manage");
		mv.addObject("type", type);
		listrecord(request,mv);
		return mv;
	}
	
	@SuppressWarnings("unchecked")
	public String listrecord(HttpServletRequest request, ModelAndView mv)
	{
		accountRecordManageService = new AccountRecordManageService_Impl();
		
		//个人账户
		String tel = request.getParameter("tel");
		String Paccount = request.getParameter("Paccount");
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}
		
		int pageIndex = 0;
		int totalRecord = 0;
		if (page != null)
		{
			pageIndex = Integer.parseInt(page);
		}
		
		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr1={"tel","like",tel};
		Object[] arr2={"account","like",Paccount};
		paramList.add(arr1);
		paramList.add(arr2);
		
		String sql1 = " SELECT\n" +
					  " ait.*, aut.name,\n" +
					  " aut.account, aut.tel\n" +
					  " FROM\n" +
					  " account_info_t ait\n" +
					  " LEFT JOIN account_user_t aut ON aut.id = ait.accountid\n" +
					  " WHERE\n" +
					  " aut.type = 0\n" +
					  " AND aut.del = 0";
		
		sql1 = accountRecordManageService.getwhereSQL(sql1, paramList, sortfield);
		
		//获取分页查询的数据结果
		totalRecord = accountRecordManageService.getCount(sql1, paramList);
		
		List<AccountInfoModel> privateInfoList = accountRecordManageService.getList(
				AccountInfoModel.class, paramList, sql1, pageIndex, 15, sortfield);
		
		//设置后台用户界面所需的其他数据
		accountRecordManageService.setListData(request,mv,pageIndex,totalRecord,privateInfoList);
		
		//公司账户
		String Caccount = request.getParameter("Caccount");
		String accountid = request.getParameter("accountid");//公司
		String page1 = request.getParameter("page1");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}
		
		int pageIndex1 = 0;
		int totalRecord1 = 0;
		if (page1 != null)
		{
			pageIndex1 = Integer.parseInt(page1);
		}
		
		List<Object[]> paramList1 = new ArrayList<Object[]>();
		Object[] arr3={"account","=",Caccount};
		Object[] arr4={"accountid","=",accountid};
		paramList1.add(arr3);
		paramList1.add(arr4);
		
		String sql2 = " SELECT\n" +
				  	  " ait.*, aut.name,\n" +
				  	  " aut.account, aut.tel, aut.comment\n" +
				  	  " FROM\n" +
				  	  " account_info_t ait\n" +
				  	  " LEFT JOIN account_user_t aut ON aut.id = ait.accountid\n" +
				  	  " WHERE\n" +
				  	  " aut.type = 1\n" +
				  	  " AND aut.del = 0";
		
		sql2 = accountRecordManageService.getwhereSQL(sql2, paramList1, sortfield);
		
		//获取分页查询的数据结果
		totalRecord1 = accountRecordManageService.getCount(sql2, paramList1);
		
		List<AccountInfoModel> companyInfoList = accountRecordManageService.getList(
				AccountInfoModel.class, paramList1, sql2, pageIndex1, 15, sortfield);
		
		//设置后台用户界面所需的其他数据
		accountRecordManageService.setListData1(request,mv,pageIndex1,totalRecord1,companyInfoList);
		
		return sql1+":"+sql2;
		
	}
	
	@RequestMapping(value = "download_account_record", method = { RequestMethod.GET,
			RequestMethod.POST })
	public void downloadAccountRecord(HttpServletRequest request,HttpServletResponse response) {
		
		String export = request.getParameter("export");
		
		ModelAndView mv = new ModelAndView("account_record_manage");
		String sql[] = listrecord(request,mv).split(":");
		
		if("0".equals(export)){
			String sql1 = null;
			sql1 = sql[0];
			
			accountRecordManageService.doDownloadAction(request, response, sql1);
		}else if("1".equals(export)){
			String sql2 = null;
			sql2 = sql[1];
			
			accountRecordManageService.doDownloadAction(request, response, sql2);
		}
	}
	
}